package com.ykdl.member.kid.prefs;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;

import com.ykdl.member.kid.sqlite.LogonCookeDao;
import com.ykdl.member.kid.sqlite.LogonRect;

/**
 * 记录程序中的一些小的数据
 * 
 * @param result
 */
public class MyPrefs {
	private static SharedPreferences settings;
	private static SharedPreferences.Editor editor;
	private static void init(Context ctx) {
		if (editor == null) {
			if (settings == null) {
				settings = ctx.getSharedPreferences("lamayuerApp", Context.MODE_PRIVATE);
				editor = settings.edit();
				Calendar calendar = Calendar.getInstance();
				editor.putString("loginday", calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1)
						+ "-" + calendar.get(Calendar.DAY_OF_MONTH));
			} else {
				editor = settings.edit();
			}
		}
	}
	
	public static void increateForceExit(Context context) {
		init(context);
		Date d=new Date();
		String key="exit"+d.getYear()+"-"+d.getMonth()+"-"+d.getDate();
		editor.putLong(key, settings.getLong(key, 0) + 1);
		editor.commit();
	}

	public static long getForceExit(Context context) {
		Date d=new Date();
		String key="exit"+d.getYear()+"-"+d.getMonth()+"-"+d.getDate();
		return settings.getLong(key, 0);
	}


	


	// 融云toke
		public static void setweb_socket(Context context, String OsToke) {
			init(context);
			editor.putString("web_socket_url", OsToke);
			editor.commit();
		}

		public static String getweb_socket(Context context) {
			init(context);
			return settings.getString("web_socket_url", "");
		}



	// 拍照的路径
	public static void setCamePath(Context context, String path) {
		init(context);
		editor.putString("curcame", path);
		editor.commit();
	}

	public static String getCamePath(Context context) {
		init(context);
		return settings.getString("curcame", "");
	}

	// 拍照的路径
	public static void setSmallCamePath(Context context, String path) {
		init(context);
		editor.putString("smallcurcame", path);
		editor.commit();
	}

	public static String getSmallCamePath(Context context) {
		init(context);
		return settings.getString("smallcurcame", "");
	}
	// 消息个数
		public static void setMessageCount(Context context, String message) {
			init(context);
			editor.putString("hasmessage", message);
			editor.commit();
		}

		public static String getMessageCount(Context context) {
			init(context);
			return settings.getString("hasmessage", null);
		}

	// 私信个数
	public static void setChatLetterCount(Context context, String message) {
		init(context);
		editor.putString("ChatLetter", message);
		editor.commit();
	}

	public static String getChatLetterCount(Context context) {
		init(context);
		return settings.getString("ChatLetter", null);
	}

	
	
	
	public static void setErrorHttp(Context context, String content) {
		init(context);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		if(content.length()>0){
			editor.putString("errorhttp",getErrorHttp(context)+"$"+sf.format(new Date())+content);
		}else{
			editor.clear();
		}
		
		editor.commit();
	}

	public static String getErrorHttp(Context context) {
		init(context);
		return settings.getString("errorhttp", "");
	}
	public static String getAnonymous(Context context) {
		LogonCookeDao dao=new LogonCookeDao(context);
		LogonRect bean=dao.fetchAllData();		
		dao.close();
		return bean.isAnonymous;
	}

	public static void setAnonymous(Context context, String isAnonymous) {
//		init(context);
//		editor.putString(SHOWNEW, clientVersion);
//		editor.commit();
		LogonCookeDao dao=new LogonCookeDao(context);
		LogonRect bean=dao.fetchAllData();
		bean.isAnonymous=isAnonymous;
		dao.insertData(bean);
		dao.close();
	}
	public static String getShowNew(Context context) {
		LogonCookeDao dao=new LogonCookeDao(context);
		LogonRect bean=dao.fetchAllData();		
		dao.close();
		return bean.welcome;
	}

	public static void setShowNew(Context context, String clientVersion) {
//		init(context);
//		editor.putString(SHOWNEW, clientVersion);
//		editor.commit();
		LogonCookeDao dao=new LogonCookeDao(context);
		LogonRect bean=dao.fetchAllData();
		bean.welcome=clientVersion;
		dao.insertData(bean);
		dao.close();
	}
	public static void putPushSetting(Context mContext,String id,String data) {
		SharedPreferences settings = mContext.getSharedPreferences("PushMsg", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(id,data);
		editor.commit();
	}
	public static String getputPushSetting(Context mContext,String id) {
		SharedPreferences settings = mContext.getSharedPreferences("PushMsg", 0);
		String data = settings.getString(id,"");
		return data;
		
	}
	public static void putCircleRead(Context mContext,int circle_id) {
		SharedPreferences settings = mContext.getSharedPreferences("CircleRead", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("CircleRead",circle_id);
		editor.commit();
	}
	public static int getCircleRead(Context mContext) {
		SharedPreferences settings = mContext.getSharedPreferences("CircleRead", 0);
		int circle_id = settings.getInt("CircleRead", 0);
		return circle_id;
		
	}
}
